//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2013 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_AUTO_TEMP_PTR_H_
#define _BUOLA_AUTO_TEMP_PTR_H_

#include <buola/auto/memory.h>

namespace buola {

///\addtogroup memory
///@{

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// temporary pointer
///
/// \c temp_ptr should be used to hold pointers to objects when we are sure that the lifetime of the object is longer than
/// the lifetime of this pointer.
///
/// Provides automatic conversion from smart and regular pointers, and automatic conversion to regular pointers.
///
/// It does not free the pointed-to object on destruction, or does any kind of reference-counting
///
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template<class tType>
class temp_ptr
{
public:
    ///all smart pointers must have this typedef
    typedef tType element_type;

    ///move constructor
    template<typename tOther>
    temp_ptr(const tOther &pO)
        :   mPtr(get_raw_pointer(pO))
    {}
    
    ///resets the scoped pointer to point to a new object \p pPtr
    ///
    ///If the scoped pointer was already pointing to some object, the object is deleted.
    template<typename tOther>
    void reset(const tOther &pO) // never throws
    {
        mPtr=get_raw_pointer(pO);
    }


    ///resets the scoped pointer to point to a new object \p pPtr
    ///
    ///Equivalent to \ref reset.
    template<typename tOther>
    const temp_ptr &operator=(const tOther &pPtr) // never throws
    {
        reset(pPtr);
        return *this;
    }

    ///returns a reference to the pointed-to object
    tType &operator*() const // never throws
    {
        return *mPtr;
    }

    ///returns a raw pointer to the pointed-to object
    tType *operator->() const // never throws
    {
        return mPtr;
    }

    ///returns a raw pointer to the pointed-to object
    tType *get() const // never throws
    {
        return mPtr;
    }

    ///explicit conversion to bool
    ///
    ///Returns true if the pointer points to some object.
    explicit operator bool() const // never throws
    {
        return mPtr;
    }

private:
    tType *mPtr;
};

///@}

/*namespace buola*/ }

#endif
